package com.iwhalecloud.bss.kite.cucc.common.util;

/**
 * Classname     :  XMLOject
 * Description   :  XML数据封装类
 * @Author       :  arthur xu
 * Data          :  2007-03-01
 */

/*import org.jdom.Document;
import org.jdom.output.XMLOutputter;
import org.jdom.CDATA;*/

import org.jdom2.CDATA;
import org.jdom2.Document;
import org.jdom2.output.XMLOutputter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

public class XMLLinkedObject {
    public String name;

    public LinkedHashMap hmAttrib;

    public LinkedHashMap props;

    @SuppressWarnings("rawtypes")
    public XMLLinkedObject(String name) {
        this.name = name;
        props = new LinkedHashMap();
    }

    @SuppressWarnings("rawtypes")
    public XMLLinkedObject(String name, LinkedHashMap hmAttrib) {
        this.name = name;
        this.hmAttrib = hmAttrib;
        props = new LinkedHashMap();
    }

    @SuppressWarnings("unchecked")
    public void addProd(String prodName, String prodValue) {
        if (props.get(prodName) == null) {
            props.put(prodName, prodValue);
        }
        else {
            if (props.get(prodName) instanceof ArrayList) {
                ArrayList list = (ArrayList) props.get(prodName);
                list.add(prodValue);
                props.put(prodName, list);
            }
            else {
                ArrayList list = new ArrayList();
                list.add(props.get(name));
                list.add(prodValue);
                props.put(prodName, list);
            }
        }
    }

    public void addProd(String prodName, Document doc) throws IOException {
        if (props.get(prodName) == null) {
            XMLOutputter outputter = new XMLOutputter();
            CDATA value = new CDATA(outputter.outputString(doc));
            props.put(prodName, value);
        }
        else {
            if (props.get(prodName) instanceof ArrayList) {
                ArrayList list = (ArrayList) props.get(prodName);
                XMLOutputter outputter = new XMLOutputter();
                CDATA value = new CDATA(outputter.outputString(doc));
                list.add(value);
                props.put(prodName, list);
            }
            else {
                ArrayList list = new ArrayList();
                list.add(props.get(name));
                XMLOutputter outputter = new XMLOutputter();
                CDATA value = new CDATA(outputter.outputString(doc));
                list.add(value);
                props.put(prodName, list);
            }
        }
    }

    public void addProd(String prodName, LinkedHashMap map) {
        if (props.get(prodName) == null) {
            props.put(prodName, map);
        }
        else {
            if (props.get(prodName) instanceof ArrayList) {
                ArrayList list = (ArrayList) props.get(prodName);
                list.add(map);
                props.put(prodName, list);
            }
            else {
                ArrayList list = new ArrayList();
                list.add(props.get(prodName));
                list.add(map);
                props.put(prodName, list);
            }
        }
    }

    public String getName() {
        return name;
    }

    public LinkedHashMap getHmAttrib() {
        return hmAttrib;
    }

    public LinkedHashMap getProds() {
        return props;
    }

    public void delProp(String prodName) {
        props.remove(prodName);
    }

    public void clear() {
        props.clear();
    }
}
